<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
</head>
<body style="height: 750px;background-color: white">
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">单位</label>-->
<!--            <div class="layui-input-block">-->
<!--                <select lay-verify="required" lay-search id="gongsi">-->
<!--                    <option id="eee" disabled selected></option>-->
<!--                </select>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">手机号码</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" name="phone" required  lay-verify="required" placeholder="请输入手机号码" autocomplete="off" class="layui-input">-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">姓名</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" name="realName" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="juese" lay-filter="juese">
                    <option id="fff" disabled selected></option>
                </select>
            </div>
        </div>
        <!--<div class="layui-form-item" id="sss" style="display: none">-->
        <!--<label class="layui-form-label">所属单位</label>-->
        <!--<div class="layui-input-block">-->
        <!--<select lay-verify="required" lay-search id="danwei" lay-filter="danwei">-->
        <!--<option id="eee" disabled selected></option>-->
        <!--</select>-->
        <!--</div>-->
        <!--</div>-->
        <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">单位</label>-->
        <!--<div class="layui-input-block">-->
        <!--<select lay-verify="required" lay-search id="danwei">-->
        <!--<option value="1">机关</option>-->
        <!--<option value="2">团体</option>-->
        <!--<option value="3">企业</option>-->
        <!--<option value="4">法人</option>-->
        <!--</select>-->
        <!--</div>-->
        <!--</div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>启用</div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>禁用</div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>
<script>
    layui.use(['form' , 'jquery'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        //添加操作
        form.on('submit(addWuye)',function(data){
            //data.field.gongsi=$("#gongsi").find("option:selected").val();
            data.field.role = $("#juese").find("option:selected").val();
            $.ajax({
                url: HOST + "user/addUser"
                ,type:"POST"
                ,contentType: 'application/json'
                ,data:JSON.stringify(data.field)
                ,success:function (data) {
                    if(data.code==2){
                        layer.msg('用户名已存在，请重新输入！');
                    }else if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('添加成功');
                        setTimeout(function(){
                            window.location.reload();
                        },3000);
                    }else{
                        layer.msg('拒绝访问');
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
            return false;
        });

        //下拉列表监听事件
        // form.on('select(juese)', function(data){
        //     if(data.value == 1376527311578304514 || data.text=="报件"){
        //         // console.log("选中报件了");
        //         $("#sss").css('display','block');
        //     }else{
        //         // console.log("啥也不是");
        //         $("#sss").css('display','none');
        //     }
        // });

        //下拉列表选项渲染
        // const selectRender = function (data) {
        //     console.log('select render');
        //     console.log(data)
        //     let _html = '';
        //     $.each(data, function (i, v) {
        //         _html += `<option name="shenbaoDW" value="${v.id}">${v.gsName}</option>`;
        //         //console.log(_html)
        //     });
        //     $('#danwei').append(_html);
        //     form.render(); // 切记：一定要对form 进行重新渲染
        // };
        // //页面加载下拉列表选项
        // $.ajax({
        //     url: "/select/gongsi_list",
        //     success: function (res) {
        //         //console.log(res.data)
        //         selectRender(res.data);
        //     }
        // })

        //遍历角色下选框
        $.ajax({
            url: HOST + "admin/role/selRole"
            , type: "POST"
            , contentType: 'application/json'
            , data: {'status': 1}
            , success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#juese').append(new Option(item.roleName, item.roleId));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });
    });
</script>
</body>
</html>